package com.dataseek.iot.device.manager.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.device.manager.vo.CategoryVo;
import com.dataseek.iot.mysql.entity.Category;
import com.dataseek.iot.mysql.service.impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.*;

/**
 * @author cc
 * @Title: 类别Handler
 * @Description: 类别Handler
 * @date 2021/9/199:49 上午
 * @since 1.0
 */
@Service
public class CategoryHandler extends ServiceHandler<Category> {

    @Autowired
    CategoryServiceImpl categoryService;
    
    
    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        Boolean isSaved =  categoryService.save((Category) entity);
        return this.createResponse(isSaved);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {
        Boolean isSaved =  categoryService.saveBatch(lists);
        return this.createResponse(isSaved);
    }

    @Override
    public Mono<ResponseEntity> removeById(String id) {
        Boolean isRemoved =  categoryService.removeById(id);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        Boolean isRemoved = categoryService.removeByIds(ids);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        Boolean isRemoved = categoryService.remove(wrapper);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        Boolean isRemoved = categoryService.removeById(id);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        Boolean isUpdated = categoryService.updateById((Category) entity);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean isUpdated = categoryService.update(wrapper);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
        Boolean isUpdated = categoryService.updateBatchById(entities);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        Category p = categoryService.getById(id);
        return this.createResponse(p);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page page = categoryService.page(p);
        return this.createResponse(page);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page page = categoryService.page(p, wrapper);
        List<Category> list = page.getRecords();
        if(list.size()>0){
            for(Category c : list){
                createCategoryVo(c);
            }
        }
        ResponseEntity entity = new ResponseEntity();
        if(Objects.isNull(result)){
            entity.setCode(ResponseConst.SERVER_ERROR_CODE);
            entity.setMsg(ResponseConst.SERVER_ERROR);
            entity.setData("");
        }else {
            List<CategoryVo> finalResult = new ArrayList<>();
            finalResult.addAll(result);
            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            page.setRecords(finalResult);
            entity.setData(page);
        }
        result.clear();
        return Mono.just(entity);
    }


    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<Category> categories = categoryService.listByIds(ids);
        return this.createResponse(categories);
    }


    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List<Category> list = categoryService.list(wrapper);
        if(list.size()>0){
            for(Category c : list){
                createCategoryVo(c);
            }
        }
        ResponseEntity entity = new ResponseEntity();
        if(Objects.isNull(result)){
            entity.setCode(ResponseConst.SERVER_ERROR_CODE);
            entity.setMsg(ResponseConst.SERVER_ERROR);
            entity.setData("");
        }else {
            List<CategoryVo> finalResult = new ArrayList<>();
            finalResult.addAll(result);
            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            entity.setData(finalResult);
        }
        result.clear();
        return Flux.just(entity);
    }



    @Override
    public Flux<ResponseEntity> findAll() {
        List<Category> list = categoryService.list();
        if(list.size()>0){
            for(Category c : list){
                createCategoryVo(c);
            }
        }
        ResponseEntity entity = new ResponseEntity();
        if(Objects.isNull(result)){
            entity.setCode(ResponseConst.SERVER_ERROR_CODE);
            entity.setMsg(ResponseConst.SERVER_ERROR);
            entity.setData("");
        }else {
            List<CategoryVo> finalResult = new ArrayList<>();
            finalResult.addAll(result);
            entity.setCode(ResponseConst.SUCCESS_CODE);
            entity.setMsg(ResponseConst.SUCCESS);
            entity.setData(finalResult);
        }
        result.clear();
        return Flux.just(entity);
    }



   private List<CategoryVo> result = new ArrayList<>();

    /**
     * 创建类别对象
     * @param c
     * @return
     */
    private List<CategoryVo> createCategoryVo(Category c){
        Object parentId = c.getParentId();
        CategoryVo categoryVo = assembleCategoryVo(c);
        if(Objects.isNull(parentId)){
            CategoryVo deepVo = findByParentId(categoryVo);
            result.add(deepVo);
        }
        return result;
    }

    /**
     * 递归深层结构
     * @param co
     * @return
     */
    private CategoryVo findByParentId(CategoryVo co){
        Object parentId = co.getInnerCategoryId();
        if(Objects.isNull(parentId)) result.add(co);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id", parentId);
        List<Category> list = categoryService.list(wrapper);
        List<CategoryVo> children = co.getChildren();
        if(list.size()>0){
            for (Category c : list){
                CategoryVo categoryVo =  assembleCategoryVo(c);
                CategoryVo deepCo     =  findByParentId(categoryVo);
                children.add(deepCo);
                co.setChildren(children);
            }
        }
        return co;
    }

    private CategoryVo assembleCategoryVo(Category c){
        JSONObject jo = (JSONObject) JSON.toJSON(c);
        jo.put("children", new JSONArray());
        CategoryVo categoryVo = JSON.parseObject(jo.toJSONString(), CategoryVo.class);
        return categoryVo;
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }
}
